<?php

namespace app\admin\controller;

use app\admin\service\LabelService;
use support\spark\model\user\UserLabel;
use support\Request;
use support\View;

class LabelController extends Curd
{
    /**
     * 获取列表
     */
    public function index()
    {
        if (request()->isAjax()) {
            $labelService = new LabelService();
            $res = $labelService->getList(request()->all());
            return sparkSuccess($res);
        }
        return view('label/index');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $labelService = new LabelService();
            $res = $labelService->addLabel($param);
            return  sparkSuccess($res);
        }
        return view('label/add');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $labelService = new LabelService();
            $res = $labelService->editLabel($param);
            return sparkSuccess($res);
        }
        $id = request()->input('id');
        $labelModel = new UserLabel();
        View::assign([
            'info' => $labelModel->getInfoById($id)
        ]);
        return view('label/edit');
    }
    /**
     * 删除
     */
    public function del(Request $request)
    {
        $id = $request->input('id');
        $labelModel = new UserLabel();
        $labelModel->delById($id);
        return sparkSuccess(dataReturn(0,'删除成功'));
    }
}